<?php
/********************
 *@param function:訂單狀態 model
 *@param datetime 2011-6-7
 *@param author abin
 ********************/
class Productunits_mdl extends CI_model{
  //操作的表.
  const TAB_NAME='clickrcms_products_units';
  function __construct(){
    parent::__construct();
    log_message('debug','productunits_mdl Model class Initialized');
  }

  /****
   *查詢所settings,加分页
   *$data :查询参数.
   */
  public function get_productunits($data=array()){
    if(!empty($data)){
      extract($data);
      isset($field) && isset($sort)?$this->db->order_by($field.' '.$sort):$this->db->order_by('id ASC');
      isset($limits) && isset($offset)?$this->db->limit($limits,$offset):'';
      isset($id) && !empty($id)?$this->db->where('id',$id):'';
      isset($language_id) && !empty($language_id)?$this->db->where('language_id',$language_id):'';
      isset($name) && !empty($name)?$this->db->like('name',$name):'';
      return $this->db->get(self::TAB_NAME);
    }
    
  }
  /****
   * 統計所有的productunits
   */
  public function totle_productunits($data=array()){
    if(!empty($data)){
      extract($data);
      isset($language_id) && !empty($language_id)?$this->db->where('language_id',$language_id):'';
      isset($name) && !empty($name)?$this->db->like('name',$name):'';
      return $this->db->count_all_results(self::TAB_NAME);
    }else{
      return $this->db->count_all(self::TAB_NAME);
    }
  }
  /*****
   * 根據key來刪除
   */
  public function delete_productunits($delete_id=''){
    if(!empty($delete_id)){
      $this->db->where("id",$delete_id);
      $this->db->delete(self::TAB_NAME);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  /*****
   * 根據ID數組來刪除 批量刪除
   */
  public function delete_batch($batch=array()){
    if(!empty($batch)){
      $this->db->where_in("id",$batch);
      $this->db->delete(self::TAB_NAME);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  /****
   *根据ID来查询得到一个信息.
   *$id :要查詢的ID
   */
  public function get_productunits_by_id($id=''){
     if(!empty($id)){
        $data=array();
        $this->db->where('id',$id);
        $get_result=$this->db->get(self::TAB_NAME);
        if($get_result->num_rows()>0)
        {
          $data=$get_result->row_array();
        }
        $get_result->free_result();
        return $data;
      }
  }

  /***
   * 修改一個productunits
   * @param array $data
   * @param int  $c_id
   * return :受影响的行数
   */
  public function edit_productunits($data=array(),$b_id=0){
    if(!empty($data) && !empty($b_id)){
      extract($data);
      //描述.
      if(isset($descriptions) && is_array($descriptions)){
        $this->db->where('id',$b_id);
        $this->db->delete(self::TAB_NAME);
        foreach ($descriptions as $language_id => $value) {
          $value['id']=$b_id;
          $value['language_id']=$language_id;
          $this->db->insert(self::TAB_NAME,$value);
        } //end foreach;
      }
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  /***
   * 新增一個 productunits
   */
  public function add_productunits($data=array()){
    if(!empty($data)){
      extract($data);
      //描述.
      if(isset($descriptions) && is_array($descriptions)){
        foreach ($descriptions as $language_id => $value) {
          $value['language_id']=$language_id;
          if(isset($insert_id) && intval($insert_id)>0)
            $value['id']=$insert_id;
          $this->db->insert(self::TAB_NAME,$value);
          $insert_id=$this->db->insert_id(); //獲取新增id
        } //end foreach;
      }
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  
  //根據id 獲取 多語言的descriptions
  public function get_descriptions($id=0){
    if(!empty($id)){
        $this->db->where('id',$id);
        return $this->db->get(self::TAB_NAME);
    }
  }  
}
